﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Xml;
using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework.Services;
using ProjectBase.Guidance.Helpers;
using Action = Microsoft.Practices.RecipeFramework.Configuration.Action;
using Conf = Microsoft.Practices.RecipeFramework.Configuration;


namespace ProjectBase.Guidance.Actions
{
    /// <summary>
    /// This is a sample ActionCoordinatorWeb that can be used to control action execution
    /// Specify its type at the Actions level like so:
    ///   <Actions CoordinatorServiceType="ActionCoordinatorWeb">
    /// Executes all actions until it finds a Continue="false" attribute on an action
    /// Execution resumes at the next Action with a Continue="true"
    /// Both these 'controlling' actions are executed too
    /// </summary>
    public class ActionCoordinatorWeb : SitedComponent, IActionCoordinationService
    {
        public static bool Continue;

        #region IActionCoordinationService Members

        public void Run(Dictionary<string, Action> declaredActions, XmlElement coordinationData)
        {
            Continue = true;
            var uiService = GetService<IUIService>(false);
            try
            {
                bool cont = true;
                var aes = GetService<IActionExecutionService>(true);
                var ds = GetService<IDictionaryService>(true);
                foreach (Action a in declaredActions.Values)
                {
                    if (!Continue)
                    {
                        break;
                    }
                    if (cont)
                    {
                        aes.Execute(a.Name);
                        cont = Utils.SafeToBool(Utils.Evaluate
                                                    (Utils.AttributeByName(a.AnyAttr, "Continue"), ds), cont);
                    }
                    else
                    {
                        cont = Utils.SafeToBool(Utils.Evaluate
                                                    (Utils.AttributeByName(a.AnyAttr, "Continue"), ds), cont);
                        if (cont)
                        {
                            aes.Execute(a.Name);
                        }
                    }
                }
                MessageBox.Show("TODO list: \n\r 1) Search the 'SQLServer' solution folder for "
                                + "\n\r   Create and Insert SQL Scripts. Execute them."
                                + "\n\r 2) Select Web project and right click on it, "
                                + "\n\r  confirm 'Set as Start up project'. Similarly for 'Index.htm'. F5 "
                                + "\n\r 3) To add new Entity's infrastructure use the Guidance recipes: "
                                + "\n\r  right click on Projects and folders, call actions from context menu."
                                , "Enjoy Catharsis");
            }
            catch (Exception ex)
            {
#if DEBUG
                VisualStudioHelper.ShowMessage(uiService, ex.ToString());
#else
                VisualStudioHelper.ShowMessage(uiService, ex.Message);
#endif
            }
        }

        #endregion
    }
}